home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJINC106.ARJ / EDITBUF.H < prev    next >
C/C++ Source or Header  |  1992-03-29  |  6KB  |  174 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. //
  13. //    You should have received a copy of the GNU Library General Public
  14. //    License along with this library; if not, write to the Free
  15. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  
  17. #ifndef _EDITBUF_H
  18. #define _EDITBUF_H
  19. #ifdef __GNUG__
  20. #pragma interface
  21. #endif
  22. #include <stdio.h>
  23. #include <fstream.h>
  24.  
  25. typedef unsigned long mark_pointer;
  26. // At some point, it might be nice to parameterize this code
  27. // in terms of buf_char.
  28. typedef /*unsigned*/ char buf_char;
  29.  
  30. // Logical pos from start of buffer (does not count gap).
  31. typedef long buf_index;
  32.             
  33. // Pos from start of buffer, possibly including gap_size.
  34. typedef long buf_offset; 
  35.  
  36. #if 0
  37. struct buf_cookie {
  38.     FILE *file;
  39.     struct edit_string *str;
  40.     struct buf_cookie *next;
  41.     buf_index tell();
  42. };
  43. #endif
  44.  
  45. struct edit_buffer;
  46. struct edit_mark;
  47.  
  48. // A edit_string is defined as the region between the 'start' and 'end' marks.
  49. // Normally (always?) 'start->insert_before()' should be false,
  50. // and 'end->insert_before()' should be true.
  51.  
  52. struct edit_string {
  53.     struct edit_buffer *buffer; // buffer that 'start' and 'end' belong to
  54.     struct edit_mark *start, *end;
  55.     int length() const; // count of buf_chars currently in string
  56.     inline edit_string(struct edit_buffer *b,
  57.               struct edit_mark *ms, struct edit_mark *me)
  58.     { buffer = b; start = ms; end = me; }
  59. /* Make a fresh, contiguous copy of the data in STR.
  60.    Assign length of STR to *LENP.
  61.    (Output has extra NUL at out[*LENP].) */
  62.     buf_char *copy_bytes(int *lenp) const;
  63. //    FILE *open_file(char *mode);
  64.     void assign(struct edit_string *src); // copy bytes from src to this
  65. };
  66.  
  67. struct edit_streambuf : public streambuf {
  68.     friend edit_buffer;
  69.     edit_string *str;
  70.     edit_streambuf* next; // Chain of edit_streambuf's for a edit_buffer.
  71.     short _mode;
  72.     edit_streambuf(edit_string* bstr, int mode);
  73.     ~edit_streambuf();
  74.     virtual int underflow();
  75.     virtual int overflow(int c = EOF);
  76.     virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
  77.     void flush_to_buffer();
  78.     void flush_to_buffer(edit_buffer* buffer);
  79.     int _inserting;
  80.     int inserting() { return _inserting; }
  81.     void inserting(int i) { _inserting = i; }
  82. //    int delete_chars(int count, char* cut_buf); Not implemented.
  83.     int truncate();
  84.     int is_reading() { return gptr() != NULL; }
  85.     buf_char* current() { return is_reading() ? gptr() : pptr(); }
  86.     void set_current(char *p, int is_reading);
  87.   protected:
  88.     void disconnect_gap_from_file(edit_buffer* buffer);
  89. };
  90.  
  91. // A 'edit_mark' indicates a position in a buffer.
  92. // It is "attached" the text (rather than the offset).
  93. // There are two kinds of mark, which have different behavior
  94. // when text is inserted at the mark:
  95. // If 'insert_before()' is true the mark will be adjusted to be
  96. // *after* the new text.
  97.  
  98. struct edit_mark {
  99.     struct edit_mark *chain;
  100.     mark_pointer _pos;
  101.     inline int insert_before() { return _pos & 1; }
  102.     inline unsigned long index_in_buffer(struct edit_buffer *buffer)
  103.     { return _pos >> 1; }
  104.     inline buf_char *ptr(struct edit_buffer *buf);
  105.     buf_index tell();
  106.     edit_mark() { }
  107.     edit_mark(struct edit_string *str, long delta);
  108.     edit_buffer *buffer();
  109.     ~edit_mark();
  110. };
  111.  
  112. // A 'edit_buffer' consists of a sequence of buf_chars (the data),
  113. // a list of edit_marks pointing into the data, and a list of FILEs
  114. // also pointing into the data.
  115. // A 'edit_buffer' coerced to a edit_string is the string of
  116. // all the buf_chars in the buffer.
  117.  
  118. // This implementation uses a conventional buffer gap (as in Emacs).
  119. // The gap start is defined by de-referencing a (buf_char**).
  120. // This is because sometimes a FILE is inserting into the buffer,
  121. // so rather than having each putc adjust the gap, we use indirection
  122. // to have the gap be defined as the write pointer of the FILE.
  123. // (This assumes that putc adjusts a pointer (as in GNU's libc), not an index.)
  124.  
  125. struct edit_buffer {
  126.     buf_char *data; /* == emacs buffer_text.p1+1 */
  127.     buf_char *_gap_start;
  128.     edit_streambuf* _writer; // If non-NULL, currently writing stream
  129.     inline buf_char *gap_start()
  130.     { return _writer ? _writer->pptr() : _gap_start; }
  131.     buf_offset __gap_end_pos; // size of part 1 + size of gap
  132.     /* int gap; implicit: buf_size - size1 - size2 */
  133.     int buf_size;
  134.     struct edit_streambuf *files;
  135.     struct edit_mark start_mark;
  136.     struct edit_mark end_mark;
  137.     edit_buffer();
  138.     inline buf_offset gap_end_pos() { return __gap_end_pos; }
  139.     inline struct edit_mark *start_marker() { return &start_mark; }
  140.     inline struct edit_mark *end_marker() { return &end_mark; }
  141. /* these should be protected, ultimately */
  142.     buf_index tell(edit_mark*);
  143.     buf_index tell(buf_char*);
  144.     inline buf_char *gap_end() { return data + gap_end_pos(); }
  145.     inline int gap_size() { return gap_end() - gap_start(); }
  146.     inline int size1() { return gap_start() - data; }
  147.     inline int size2() { return buf_size - gap_end_pos(); }
  148.     inline struct edit_mark * mark_list() { return &start_mark; }
  149.     void make_gap (buf_offset);
  150.     void move_gap (buf_offset pos);
  151.     void move_gap (buf_char *pos) { move_gap(pos - data); }
  152.     void gap_left (int pos);
  153.     void gap_right (int pos);
  154.     void adjust_markers(mark_pointer low, mark_pointer high,
  155.             int amount, buf_char *old_data);
  156.     void delete_range(buf_index from, buf_index to);
  157.     void delete_range(struct edit_mark *start, struct edit_mark *end);
  158. };
  159.  
  160. extern buf_char * bstr_copy(struct edit_string *str, int *lenp);
  161.  
  162. // Convert a edit_mark to a (buf_char*)
  163.  
  164. inline buf_char *edit_mark::ptr(struct edit_buffer *buf)
  165.     { return buf->data + index_in_buffer(buf); }
  166.  
  167. inline void edit_streambuf::flush_to_buffer()
  168. {
  169.     edit_buffer* buffer = str->buffer;
  170.     if (buffer->_writer == this) flush_to_buffer(buffer);
  171. }
  172. #endif /* !_EDITBUF_H*/
  173.  
  174.